<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="author" content="火星科技 http://mars3d.cn " />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="http://mars3d.cn/favicon.ico" />
    <title>右键菜单 | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <!--第三方lib-->
    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,turf,mars3d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars3dContainer" class="mars3d-container"></div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        var mapOptions = mars3d.Util.merge(options, {});

        //创建三维地球场景
        map = new mars3d.Map("mars3dContainer", mapOptions);

        //演示3种不同层次的绑定方式，可以按需使用
        bindMapDemo();
        bindLayerDemo();
        bindGraphicDemo();
      }

      //1.在map地图上绑定右键菜单
      function bindMapDemo() {
        //内置的默认右键菜单获取方法
        // var defaultContextmenuItems =map.getDefaultContextMenu()
        //可以删减defaultContextmenuItems数组内值
        // map.bindContextMenu(defaultContextmenuItems)

        //解除已绑定的右键菜单
        map.unbindContextMenu();

        var mapContextmenuItems = [
          {
            text: "显示此处经纬度",
            iconCls: "fa fa-info-circle",
            show: function (e) {
              return Cesium.defined(e.cartesian);
            },
            callback: function (e) {
              var mpt = mars3d.LatLngPoint.fromCartesian(e.cartesian);
              haoutil.alert(mpt.toString(), "位置信息");
            },
          },
          {
            text: "查看当前视角",
            iconCls: "fa fa-camera-retro",
            callback: function (e) {
              let mpt = JSON.stringify(map.getCameraView());
              haoutil.alert(mpt, "当前视角信息");
            },
          },
          {
            text: "开启深度监测",
            iconCls: "fa fa-eye-slash",
            show: function () {
              return !map.scene.globe.depthTestAgainstTerrain;
            },
            callback: function (e) {
              map.scene.globe.depthTestAgainstTerrain = true;
            },
          },
          {
            text: "关闭深度监测",
            iconCls: "fa fa-eye",
            show: function () {
              return map.scene.globe.depthTestAgainstTerrain;
            },
            callback: function (e) {
              map.scene.globe.depthTestAgainstTerrain = false;
            },
          },
          {
            text: "视角切换",
            iconCls: "fa fa-street-view",
            children: [
              {
                text: "移动到此处",
                iconCls: "fa fa-send-o",
                show: function (e) {
                  return Cesium.defined(e.cartesian);
                },
                callback: function (e) {
                  let cameraDistance = Cesium.Cartesian3.distance(e.cartesian, map.camera.positionWC) * 0.1;

                  map.flyToPoint(e.cartesian, {
                    radius: cameraDistance, //距离目标点的距离
                    maximumHeight: map.camera.positionCartographic.height,
                  });
                },
              },
            ],
          },
        ];
        map.bindContextMenu(mapContextmenuItems);
      }

      //2.在layer图层上绑定右键菜单
      function bindLayerDemo() {
        var graphicLayer = new mars3d.layer.GeoJsonLayer({
          name: "标绘示例数据",
          url: "//data.mars3d.cn/file/geojson/mars3d-draw.json",
        });
        map.addLayer(graphicLayer);

        //在layer上绑定右键菜单
        graphicLayer.bindContextMenu([
          {
            text: "删除对象",
            iconCls: "fa fa-trash-o",
            callback: function (e) {
              let graphic = e.graphic;
              if (graphic) {
                graphicLayer.removeGraphic(graphic);
              }
            },
          },
          {
            text: "计算长度",
            iconCls: "fa fa-medium",
            show: function (e) {
              let graphic = e.graphic;
              return (
                graphic.type === "polyline" ||
                graphic.type === "curve" ||
                graphic.type === "polylineVolume" ||
                graphic.type === "corridor" ||
                graphic.type === "wall"
              );
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strDis = mars3d.MeasureUtil.formatDistance(graphic.distance);
              haoutil.alert("该对象的长度为:" + strDis);
            },
          },
          {
            text: "计算周长",
            iconCls: "fa fa-medium",
            show: function (e) {
              let graphic = e.graphic;
              return graphic.type === "circle" || graphic.type === "rectangle" || graphic.type === "polygon";
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strDis = mars3d.MeasureUtil.formatDistance(graphic.distance);
              haoutil.alert("该对象的周长为:" + strDis);
            },
          },
          {
            text: "计算面积",
            iconCls: "fa fa-reorder",
            show: function (e) {
              let graphic = e.graphic;
              return graphic.type === "circle" || graphic.type === "rectangle" || graphic.type === "polygon";
            },
            callback: function (e) {
              let graphic = e.graphic;
              let strArea = mars3d.MeasureUtil.formatArea(graphic.area);
              haoutil.alert("该对象的面积为:" + strArea);
            },
          },
        ]);
      }

      //3.在graphic数据上绑定右键菜单
      function bindGraphicDemo() {
        //创建矢量数据图层
        let graphicLayer = new mars3d.layer.GraphicLayer();
        map.addLayer(graphicLayer);

        let graphic = new mars3d.graphic.BoxEntity({
          position: new mars3d.LatLngPoint(116.336525, 31.196721, 323.35),
          style: {
            dimensions: new Cesium.Cartesian3(2000.0, 2000.0, 2000.0),
            fill: true,
            color: "#00ff00",
            opacity: 0.9,
            label: {
              text: "graphic绑定的演示",
              font_size: 25,
              font_family: "楷体",
              color: "#003da6",
              outline: true,
              outlineColor: "#bfbfbf",
              outlineWidth: 2,
              horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
              verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            },
          },
        });

        graphicLayer.addGraphic(graphic);

        //2.在graphic上绑定右键菜单
        graphic.bindContextMenu([
          {
            text: "删除对象[graphic绑定的]",
            iconCls: "fa fa-trash-o",
            callback: function (e) {
              let graphic = e.graphic;
              if (graphic) {
                graphic.remove();
              }
            },
          },
        ]);
      }
    </script>
  </body>
</html>
